﻿using System;
using System.Web.UI;
using Acc.Lib;
using Data.UserDb;

namespace Acc_Web
{
    public partial class Buy : Page
    {
        protected Items item;
        protected Int32 leftAmount;
        protected UserInfo usr;
        protected UserInfo toUInfo;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                usr = CookiesFunc.GetUserCookie();
                if (usr == null)
                {
                    Response.Write(String.Format("<script>alert('请先登录');window.location.href='/Index.aspx';</script>"));
                    Response.End();
                    return;
                }
                string iReqid = Request["i"];
                if (String.IsNullOrEmpty(iReqid))
                {
                    Response.Write(String.Format("<script>alert('参数出错');history.back();</script>"));
                    Response.End();
                    return;
                }
                int itemid = 0;
                Int32.TryParse(iReqid, out itemid);
                if (itemid == 0)
                {
                    Response.Write(String.Format("<script>alert('参数出错');history.back();</script>"));
                    Response.End();
                    return;
                }
                var toUserName = Request["tou"];
                toUInfo = usr;

                if(!String.IsNullOrEmpty(toUserName))
                {
                    toUInfo = new UserInfo();
                    try
                    {
                        toUInfo.GetInfoByUserName(toUserName);
                    }
                    catch
                    {
                        Response.Write(String.Format("<script>alert('该用户不存在');history.back();</script>"));
                        Response.End();
                        return;
                    }
                }

                var sr = new ShopRepository();
                var account = sr.GetAccount(usr.UserId);
                leftAmount = account == null ? 0 : account.Amount.Value;
                item = sr.GetItemDetail(itemid);
                message.Visible = leftAmount < item.Price;

                ViewState["item"] = item.Serialize();
                ViewState["tou"] = toUInfo.Serialize();
            }
        }

        protected void sendOrder_Click(object sender, ImageClickEventArgs e)
        {
            var s = ViewState["item"].ToString();
            var tu = ViewState["tou"].ToString();
            item = s.Deserialize<Items>();
            toUInfo = tu.Deserialize<UserInfo>();
            usr = CookiesFunc.GetUserCookie();

            try
            {
                var sr = new ShopRepository();
                var order = sr.Buy(item.ItemId, usr.UserId, toUInfo.UserId);
                Response.Redirect("result.aspx?oid=" + order.OrderId);
            }
            catch
            {
                Response.Write(String.Format("<script>alert('创建订单出错,请重新选择');history.back();</script>"));
                Response.End();
                return;
            }
        }
    }
}